package down.xiaoheihe.work.Shiro;

import down.xiaoheihe.work.Entity.User;
import down.xiaoheihe.work.Mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm{
    @Autowired
    private UserMapper userMapper;
    /**
     * 授权逻辑
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return new SimpleAuthorizationInfo();
    }
    /**
     * 执行认证逻辑
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) arg0;
        String userUid = token.getUsername();
        Integer uid = Integer.parseInt(userUid);
        User user = userMapper.selectByPrimaryKey(uid);
        return new SimpleAuthenticationInfo(uid,user.getPassword(), user.getUsername());
    }
}